<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\Base\DistrictModel;
use App\Models\Trade\Common\FishClassifyModel;
use App\Models\Trade\Common\FishKindModel;
use App\Http\Resources\Common\FishClassifyResource;
use App\Http\Resources\Common\FishKindResource;

class CommonController extends Controller
{

    /**
     * 获取level1、level2级的地址，并以upid分组
     */
    public function getDistrictSimplify()
    {
        $data = DistrictModel::selectBySimplify()->groupBy('upid')->toArray();
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 根据upid获取地址
     *
     * @param $upid get            
     */
    public function getDistrict(Request $request)
    {
        $this->validate($request, [
            'upid' => 'nullable|integer|min:0'
        ]);
        $upid = $request->query('upid', 0);
        
        $districtModel = new DistrictModel();
        $data = DistrictModel::selectByUpid($upid)->toArray();
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 获取所有品种
     */
    public function getFishKindAll()
    {
        $data = FishKindModel::selectAll()->toArray();
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 获取分类和品种
     */
    public function getFish()
    {
        $data['fish_classify'] = new FishClassifyResource(FishClassifyModel::selectByParentId());
        $data['fish_kind'] = new FishKindResource(FishKindModel::selectAll());
        
        return $this->response->setResponseData($data)->getResponse();
    }
}
